
**********************
* CBP Category (12') *
**********************

* DME Claims
{
use ".\statadata\dme_total_matched", clear
keep if category_x!="" & category_x!="Diabetes" & strpos(category_x, "Wheelchair")==0
gen yq = qofd(clm_thru_dt)
format yq %tq
collapse (count) n_claim = line_alowd_chrg_amt (sum) total_amt = line_alowd_chrg_amt q = line_srvc_cnt, by(bene_id hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x yq)
}

* Number of Claim in 2010 as weights & Category share as CBP treatment intensity
{
preserve
gen year = year(dofq(yq))
keep if year==2010
merge m:1 bene_id year using ".\temp\mbsf", keepusing(geo_id) keep(3) nogen

collapse (sum) payment_2010=total_amt claim_2010=n_claim, by(hcpcs_cd mod1 mod2 category_x r1_x r2_x r1rc_x geo_id)
replace r1_x = . if geo_id!="CBA57" & inlist(hcpcs_cd, "E0193", "E0277", "E0371", "E0372", "E0373")
gen t1 = payment_2010 if r1_x==1
gen t2 = payment_2010 if r2_x==1
gen t3 = payment_2010 if r1rc_x==1
collapse (sum) t1 t2 t3 payment_2010 claim_2010, by(geo_id category_x)
gen share_r1 = t1/payment_2010
gen share_r2 = t2/payment_2010
gen share_r1rc = t3/payment_2010
drop t1 t2 t3
drop if geo_id==""
save ".\temp\geo_cate_share.dta", replace
restore

collapse (sum) n_claim total_amt q, by(bene_id category_x yq)
save ".\temp\sample_category.dta", replace
}

* MBSF: Enrollment info
{
keep bene_id
duplicates drop
merge 1:m bene_id using ".\temp\mbsf", keep(3) nogen
keep bene_id year geo_id cba_round enroll* dual* 
reshape long enroll_Q dual_Q, i(bene_id year geo_id cba_round) j(qtr)
gen yq = yq(year,qtr)
format yq %tq

keep if enroll_Q==3 
gen dual = 1 if dual_Q==3
replace dual = 0 if dual_Q==0
bysort bene_id: egen temp = count(enroll_Q)
gen enroll_28 = 1 if temp==28 
keep bene_id yq geo_id cba_round dual enroll_28

merge 1:m bene_id yq using ".\temp\sample_category.dta", keep(3) nogen
save ".\temp\sample_category.dta", replace
}

* Regression Sample Construction
{
use ".\temp\sample_category.dta", clear
collapse (sum) n_claim q total_amt, by(bene_id geo_id cba_round category_x yq)
bysort bene_id category_x: egen temp = min(yq)
gen n_initiator = 1 if temp==yq
drop temp
collapse (count) N_bene = q (sum) n_initiator q total_amt n_claim, by(category_x yq geo_id cba_round)
drop if yq==.
drop if geo_id==""
drop if category_x==""
unique category_x
unique yq
unique geo_id
di 969*28*12
egen category_id = group(category_x)
save ".\temp\sample_category_reg.dta", replace

preserve
keep category_id category_x
duplicates drop
tempfile temp
save `temp'
restore
keep geo_id cba_round
duplicates drop
expand 28
bysort geo_id: gen yq = yq(2008,4)+_n
format yq %tq
expand 12
bysort geo_id yq: gen category_id = _n
merge m:1 category_id using `temp', nogen
merge 1:1 geo_id yq category_id using ".\temp\sample_category_reg.dta", nogen

merge m:1 geo_id yq using ".\temp\population", keep(1 3) nogen
merge m:1 geo_id category_x using ".\temp\geo_cate_share", keep(1 3) nogen
replace payment_2010 = 0 if payment_2010==.
replace claim_2010 = 0 if claim_2010==.

replace q = 0 if q==.
rename N_bene n_bene
replace n_bene = 0 if n_bene==.
replace n_initiator = 0 if n_initiator==.
replace total_amt = 0 if total_amt==.

gen qX = q/pop_FFS*1000
gen NbeneX = n_bene/pop_FFS*1000
gen NinitiatorX = n_initiator/pop_FFS*1000
gen paymentX = total_amt/pop_FFS*1000
	
gen lg_qX = log(qX)
gen lg_NbeneX = log(NbeneX)	
gen lg_NinitiatorX = log(NinitiatorX)	
gen lg_paymentX = log(paymentX)

gen price = total_amt/q
gen lg_price = log(price)

gen dd = 0
replace dd = share_r1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1
replace dd = share_r2 if yq>=yq(2013,3) & yq<=yq(2016,2) & cba_round==2
replace dd = share_r1rc if yq>=yq(2014,1) & yq<=yq(2016,4) & cba_round==1
save ".\temp\sample_category_reg.dta", replace
}


*** Regression: DID Main effect ***
{
use ".\temp\sample_category_reg.dta", clear
egen geo_idnum = group(geo_id)

local rp replace
foreach ver in price qX NbeneX NinitiatorX paymentX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010], nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA3_DIDCategory") sheetreplace	
restore
}


*** Regression: DID Main effect (population weighted) ***
{
use ".\temp\sample_category_reg.dta", clear
egen geo_idnum = group(geo_id)

local rp replace
foreach ver in price qX NbeneX NinitiatorX paymentX {
	cap n qui reghdfe lg_`ver' dd [aweight=pop_FFS_2010], nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=pop_FFS_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA4_DIDCategory_popw") sheetreplace	
restore
}





******************************
* CBP Category by Wave (12') *
******************************

* DME Claims
{
use ".\statadata\dme_total_matched", clear
keep if category_x!="" & category_x!="Diabetes" & strpos(category_x, "Wheelchair")==0
drop if category_x=="CPAP" & (r1_x==. | r2_x==.)
drop if category_x=="Enteral Nutrients" & (r1_x==. | r2_x==.)
drop if category_x=="Hospital Bed" & (r1_x==. | r2_x==.)
drop if category_x=="Oxygen Concentrator" & (r1_x==. | r2_x==.)
drop if category_x=="Walker" & (r1_x==. | r2_x==.)
drop if category_x=="Mattress" & (r1_x==. | r2_x==.)

drop if category_x=="NPWT" & r2_x==.

drop if category_x=="Commonde" & r1rc_x==.
drop if category_x=="External Infusion" & r1rc_x==.
drop if category_x=="NPWT" & r1rc_x==.
drop if category_x=="Nebulizer" & r1rc_x==.
drop if category_x=="Patient Lift" & r1rc_x==.
drop if category_x=="TENS" & r1rc_x==.

gen yq = qofd(clm_thru_dt)
format yq %tq
collapse (count) n_claim = line_alowd_chrg_amt (sum) total_amt = line_alowd_chrg_amt q = line_srvc_cnt, by(bene_id category_x r1_x r2_x r1rc_x yq)
compress
save ".\temp\sample_category2.dta", replace
}

* Number of Claim in 2010 as weights
{
preserve
gen year = year(dofq(yq))
keep if year==2010
merge m:1 bene_id year using ".\temp\mbsf", keepusing(geo_id) keep(3) nogen
collapse (sum) payment_2010=total_amt claim_2010=n_claim, by(geo_id category_x)
save ".\temp\geo_cate_share2", replace
restore
}

* MBSF: Enrollment info
{
keep bene_id
duplicates drop
merge 1:m bene_id using ".\temp\mbsf", keep(3) nogen
keep bene_id year geo_id cba_round enroll* dual* 
reshape long enroll_Q dual_Q, i(bene_id year geo_id cba_round) j(qtr)
gen yq = yq(year,qtr)
format yq %tq

keep if enroll_Q==3 
gen dual = 1 if dual_Q==3
replace dual = 0 if dual_Q==0
bysort bene_id: egen temp = count(enroll_Q)
gen enroll_28 = 1 if temp==28 
keep bene_id yq geo_id cba_round dual enroll_28

merge 1:m bene_id yq using ".\temp\sample_category2.dta", keep(3) nogen
compress
save ".\temp\sample_category2.dta", replace
}

*** Supplier ***
{
use ".\statadata\dme_total_matched", clear
keep if category_x!="" & category_x!="Diabetes" & strpos(category_x, "Wheelchair")==0
drop if category_x=="CPAP" & (r1_x==. | r2_x==.)
drop if category_x=="Enteral Nutrients" & (r1_x==. | r2_x==.)
drop if category_x=="Hospital Bed" & (r1_x==. | r2_x==.)
drop if category_x=="Oxygen Concentrator" & (r1_x==. | r2_x==.)
drop if category_x=="Walker" & (r1_x==. | r2_x==.)
drop if category_x=="Mattress" & (r1_x==. | r2_x==.)

drop if category_x=="NPWT" & r2_x==.

drop if category_x=="Commonde" & r1rc_x==.
drop if category_x=="External Infusion" & r1rc_x==.
drop if category_x=="NPWT" & r1rc_x==.
drop if category_x=="Nebulizer" & r1rc_x==.
drop if category_x=="Patient Lift" & r1rc_x==.
drop if category_x=="TENS" & r1rc_x==.

gen yq = qofd(clm_thru_dt)
format yq %tq
gen year = year(dofq(yq))
merge m:1 bene_id year using ".\temp\mbsf", keepusing(geo_id) keep(1 3) nogen

collapse (count) n_claim = line_alowd_chrg_amt (sum) total_amt = line_alowd_chrg_amt, by(prvdr_npi yq geo_id category_x)
bysort geo_id yq category_x: egen total_total_amt = sum(total_amt)
gen share = total_amt/total_total_amt
gen share2 = share^2
gen n = 1 if share>0.01
collapse (sum) N_NPI = n HHI_NPI = share2, by(geo_id yq category_x)
drop if geo_id==""
drop if yq==.
save ".\temp\sample_category_supplier2", replace
}

* Regression Sample Construction
{
use ".\temp\sample_category2.dta", clear
collapse (sum) n_claim q total_amt, by(bene_id geo_id cba_round category_x yq)
bysort bene_id category_x: egen temp = min(yq)
gen n_initiator = 1 if temp==yq
drop temp
collapse (count) N_bene = q (sum) n_initiator q total_amt n_claim, by(category_x yq geo_id cba_round)
drop if yq==.
drop if geo_id==""
drop if category_x==""
unique category_x
unique yq
unique geo_id
di 969*28*12
egen category_id = group(category_x)
save ".\temp\sample_category_reg2.dta", replace

preserve
keep category_id category_x
duplicates drop
tempfile temp
save `temp'
restore
keep geo_id cba_round
duplicates drop
expand 28
bysort geo_id: gen yq = yq(2008,4)+_n
format yq %tq
expand 12
bysort geo_id yq: gen category_id = _n
merge m:1 category_id using `temp', nogen
merge 1:1 geo_id yq category_id using ".\temp\sample_category_reg2.dta", nogen
merge 1:1 geo_id yq category_x using ".\temp\sample_category_supplier2.dta", nogen

merge m:1 geo_id yq using ".\temp\population", keep(1 3) nogen
merge m:1 geo_id category_x using ".\temp\geo_cate_share2", keep(1 3) nogen
replace payment_2010 = 0 if payment_2010==.
replace claim_2010 = 0 if claim_2010==.

replace q = 0 if q==.
rename N_bene n_bene
replace n_bene = 0 if n_bene==.
replace n_initiator = 0 if n_initiator==.
replace total_amt = 0 if total_amt==.

gen qX = q/pop_FFS*1000
gen NbeneX = n_bene/pop_FFS*1000
gen NinitiatorX = n_initiator/pop_FFS*1000
gen paymentX = total_amt/pop_FFS*1000
	
gen lg_qX = log(qX)
gen lg_NbeneX = log(NbeneX)	
gen lg_NinitiatorX = log(NinitiatorX)	
gen lg_paymentX = log(paymentX)

gen price = total_amt/q
gen lg_price = log(price)

replace N_NPI = 0 if N_NPI==.
gen N_NPIX = N_NPI/pop_FFS*1000
foreach v of varlist N_NPI* HHI* {
	replace `v' = . if q==0
}

gen r1_x = 1 if inlist(category_x,"CPAP","Enteral Nutrients","Hospital Bed","Oxygen Concentrator","Walker","Mattress")
gen r2_x = 1 if r1_x==1 | category_x=="NPWT"
gen r1rc_x = 1 

gen dd = 0
replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & cba_round==1 & r1_x==1
replace dd = 0 if yq>=yq(2011,1) & yq<=yq(2013,4) & geo_id!="CBA57" & category_x=="Mattress" // Mattress, Miami only
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & cba_round==2 & r2_x==1
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & cba_round==1 & r1rc_x==1
save ".\temp\sample_category_reg2.dta", replace
}


* By wave 
{
use ".\temp\sample_category_reg2.dta", clear
egen geo_idnum = group(geo_id)
replace r1_x = . if category_x=="Mattress"
local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1, nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1 & yq<yq(2014,1), nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1_bf2014, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==2) & r2_x==1, nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R2, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1rc_x==1 & r1_x==., nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1RC, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA6_DIDCategory_Wave") sheetreplace	
restore

local rp replace
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1, nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1_x==1 & yq<yq(2014,1), nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1_bf2014, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==2) & r2_x==1, nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R2, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if (cba_round==0 | cba_round==1) & r1rc_x==1 & r1_x==., nocons absorb(geo_idnum#category_id yq#category_id) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Wave, R1RC, Category FE, X, Yr-Qtr FE, X, GEO FE, X, Category-YQ FE, X, Category-GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA10_DIDCategory_Wave_HHI") sheetreplace	
restore
}


* Geographic spillover
{
use ".\raw\ZIP_CBA_CBSA.dta", clear
keep geo_id CBA_adjacent 
duplicates drop
drop if geo_id==""

merge 1:m geo_id using ".\temp\sample_category_reg2.dta", keep(2 3) nogen
egen geo_idnum = group(geo_id)

local rp replace
foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if CBA_adjacent==., nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if CBA_adjacent==., nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}

foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if CBA_adjacent!=. | cba_round!=0, nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if CBA_adjacent!=. | cba_round!=0, nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}

replace dd = 1 if yq>=yq(2011,1) & yq<=yq(2013,4) & CBA_adjacent==1 & r1_x==1
replace dd = 0 if yq>=yq(2011,1) & yq<=yq(2013,4) & inlist(geo_id,"17500","28580","33100","34940","36380","38940")==0 & category_x=="Mattress" // Mattress, Miami only
replace dd = 1 if yq>=yq(2013,3) & yq<=yq(2016,2) & CBA_adjacent==2 & r2_x==1
replace dd = 1 if yq>=yq(2014,1) & yq<=yq(2016,4) & CBA_adjacent==1 & r1rc_x==1

foreach ver in price qX {
	cap n qui reghdfe lg_`ver' dd [aweight=claim_2010] if cba_round==0, nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
foreach ver in HHI_NPI N_NPI {
	cap n qui reghdfe `ver' dd [aweight=claim_2010] if cba_round==0, nocons absorb(geo_idnum#category_id yq#category_id geo_idnum#yq) vce(cluster geo_id)
	qui sum `ver' if e(sample) [aweight=claim_2010]
	cap n outreg2 using $resdir\reg.dta, dta addtext(Category FE, X, Yr-Qtr FE, X, GEO FE, X, SE cluster, geo, weights, claim_2010) addstat(Mean of Dep Var, r(mean)) `rp'
	local rp
}
preserve
use $resdir\reg_dta.dta, clear
export excel using $resdir\Table.xls, sheet("TableA12_DIDCategory_Adjacent") sheetreplace	
restore
}

